		TITLE	FLUSHEXE - Copyright (c) SLR Systems 1994

		INCLUDE	MACROS
		INCLUDE	EXES

		PUBLIC	FLUSH_PACK


		.DATA

		EXTERNDEF	TOTAL_RELOCS:DWORD,NEW_REPT_ADDR:DWORD,ZEROS_16:DWORD,HIGH_WATER:DWORD,HIGH_PC:DWORD
		EXTERNDEF	EXE_OUT_POSITION:DWORD

		EXTERNDEF	EXEPACK_STRUCTURE:SEQ_STRUCT,EXEHEADER:EXE


		EXTERNDEF	EXEUNPACK:PROC,EXEUNPACK_START:PROC

		EXTERNDEF	EXEPACK_PARAS:WORD,INITIAL_COUNT:WORD

		EXTERNDEF	STACK_ADR:DWORD,START_ADR:DWORD

		EXTERNDEF	PACK_LEN:ABS


		.CODE	PASS2_TEXT

		EXTERNDEF	PACK_MOVE_FINAL_HIGH:PROC,FINAL_HIGH_PARA:PROC,STORE_EAXECX_EDX_SEQ:PROC,MESOUT:PROC
		EXTERNDEF	CALC_EXE_LEN:PROC,DO_EXEHEADER:PROC,HANDLE_SLRPACK:PROC,HANDLE_QUIKPACK:PROC,DO_COMPRESS:PROC
		EXTERNDEF	DOTS_FINISH:PROC,AX_MESOUT:PROC,COMPRESSING_MESSAGE:PROC


FLUSH_PACK	PROC
		;
		;WRITE START-UP CODE (FFFF 0000)
		;
		MOV	EAX,OFF PACK_START_CODE
		MOV	ECX,4

		CALL	PACK_MOVE_FINAL_HIGH
		;
		;EXEPACK..., WELL, PARAGRAPH ALIGN FINAL_EXE
		;
if	fg_dospack
		BITT	SLRPACK_FLAG
		JNZ	L1$
endif
		CALL	FINAL_HIGH_PARA

if	fg_dospack
		JMP	L12$

L1$:
		XOR	ECX,ECX			;IF COMPRESSING, DATA IS STORED IN

		SUB	ECX,NEW_REPT_ADDR	;EXEPACK_STRUCTURE, ALIGN THAT PLEASE

		AND	ECX,0FH
		JZ	L12$

		MOV	EAX,OFF ZEROS_16
		MOV	EDX,OFF EXEPACK_STRUCTURE

		ADD	NEW_REPT_ADDR,ECX
		CALL	STORE_EAXECX_EDX_SEQ

endif

L12$:
		;
		;MOVE STUFF FROM EXEHEADER TO PACK_SEGMENT
		;
		;STACK_ADR
		;START_ADR
		;

		MOV	AX,EXEHEADER._EXE_REG_SS
		MOV	ECX,DPTR EXEHEADER._EXE_REG_IP

		MOV	WPTR STACK_ADR+2,AX

		MOV	AX,EXEHEADER._EXE_REG_SP

		MOV	WPTR STACK_ADR,AX
		MOV	START_ADR,ECX
		;
		;DO CHUNKS_64K AND CHUNK_PARTIAL
		;
		CALL	CALC_EXE_LEN

		SHR	EAX,4
		MOV	ECX,OFF EXEUNPACK


		MOV	EXEHEADER._EXE_REG_CS,AX
		SUB	ECX,OFF EXEUNPACK_START

		MOV	EXEHEADER._EXE_REG_IP,CX

		MOV	EXEPACK_PARAS,AX
		;
		;NOW MOVE PACK_SEGMENT TO FINAL_EXE
		;
		MOV	EAX,OFF EXEUNPACK_START

		MOV	ECX,PACK_LEN
		CALL	PACK_MOVE_FINAL_HIGH
		;
		;FINALIZE STUFF IN EXEHEADER
		;
		;STACK GOES AT A PARAGRAPH AT HIGH_WATER + TOTAL_RELOCS*4 + 256
		;STACK ITSELF IS OFFSET PACK_LEN+128
		;
		MOV	EAX,TOTAL_RELOCS
		MOV	EDX,HIGH_WATER

		SHL	EAX,2		;*4

		ADD	EAX,EDX

		PUSH	EAX
		CALL	CALC_EXE_LEN	;USE LARGER OF EXELEN AND HIGH_WATER

		POP	EDX

		CMP	EDX,EAX
		JB	L232$
L231$:
		MOV	EAX,EDX
L232$:
		ADD	EAX,256

		PUSH	EAX

		SHR	EAX,4
		MOV	ECX,PACK_LEN+131

		MOV	EXEHEADER._EXE_REG_SS,AX
		AND	ECX,0FFFFFFFCH

		MOV	EXEHEADER._EXE_REG_SP,CX
		POP	EAX

		ADD	EAX,ECX
		;
		;IF STACK > HIGH_PC, SET HIGH_PC
		;THEN DO MIN_ABOVE
		;
		CMP	HIGH_PC,EAX
		JAE	L25$
L24$:
		MOV	HIGH_PC,EAX
L25$:
if	fg_dospack
		BITT	SLRPACK_FLAG
		JNZ	L27$
endif

L28$:
		CALL	DO_EXEHEADER
		MOV	ECX,EXE_OUT_POSITION
		RET

if	fg_dospack

L27$:
		CALL	COMPRESSING_MESSAGE

		CALL	DO_COMPRESS

		RESS	SLRPACK_FLAG
		BITT	NO_SLRPACKING_DONE
		JNZ	L28$
		;
		CALL	FINAL_HIGH_PARA
		CALL	DOTS_FINISH
		;
		;MOVE STUFF FROM EXEHEADER TO PACK_SEGMENT
		;
		;STACK_ADR
		;START_ADR
		;
		BITT	QUIKPACK_FLAG
		JNZ	L32$
		CALL	HANDLE_SLRPACK
		JMP	L33$
L32$:
		CALL	HANDLE_QUIKPACK
L33$:

		ADD	EAX,ECX
		;
		;CHECK FOR NEW STACK OVERLAYING OLD CODE
		;
		;IF STACK > HIGH_PC, SET HIGH_PC
		;THEN DO MIN_ABOVE
		;
		CMP	HIGH_PC,EAX
		JAE	L35$
L34$:
		MOV	HIGH_PC,EAX
L35$:
		CALL	DO_EXEHEADER

		MOV	ECX,EXE_OUT_POSITION

		RET

endif

FLUSH_PACK	ENDP


		.CONST

PACK_START_CODE DW	-1,0


		END

